using System;

namespace Gebra
{
    public class Divide : Operand
    {
        private readonly Operand _numerator;
        private readonly Operand _denomenator;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_numerator">Numerator operand.</param>
        /// <param name="a_denomenator">Denomenator operand.</param>
        /// <exception cref="ArgumentNullException">Thrown if "<paramref name="a_numerator"/>" is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if "<paramref name="a_denomenator"/>" is null.</exception>
        public Divide(Operand a_numerator, Operand a_denomenator)
        {
            #region Argument Validation

            if (a_numerator == null)
                throw new ArgumentNullException("a_numerator");

            if (a_denomenator == null)
                throw new ArgumentNullException("a_denomenator");

            #endregion

            _numerator = a_numerator;
            _denomenator = a_denomenator;
        }

        /// <summary>
        /// Evaluate this operand.
        /// </summary>
        /// <returns>Result of evaluation.</returns>
        public override Operand Evaluate()
        {
            var numeratorResult = _numerator.Evaluate();
            var denomenatorResult = _denomenator.Evaluate();

            if (numeratorResult is Number && denomenatorResult is Number)
                return new Number((numeratorResult as Number).Value / (denomenatorResult as Number).Value);

            return new Divide(numeratorResult, denomenatorResult);
        }
    }
}